package com.mccree.codec2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufEncoder;

public class TestProtoClient2 {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast("encoder",new ProtobufEncoder());
                            pipeline.addLast(new TestProtoClientHandler2());
                        }
                    });
            System.out.println("Client..........ok");
            ChannelFuture future = bootstrap.connect("127.0.0.1",7002).sync();
            future.addListener(future1 -> {
               if (future1.isSuccess()){
                   System.out.println("绑定7002端口成功");
               }else {
                   System.out.println("绑定7002端口失败");
               }
            });
            future.channel().closeFuture().sync();
        }finally {
            group.shutdownGracefully();
        }
    }
}
